package fy.form.oa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.util.DBUtil;


public class SYSerialDao {
	
	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(SYSerialDao.class);

	private long summaryID;
	private String companyID;

	public void setColSummary(long summaryID) {
		this.summaryID = summaryID;
	}

	public boolean createSYSerial() {
		System.err.println("编码测试：");
		synchronized (SYSerialDao.class) {
			boolean success = false;

			Connection conn = null;
			PreparedStatement pst = null;
			ResultSet rs = null;
			try {
				conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());

				DBUtil.setAutoCommit(conn, false);
				
				pst=conn.prepareStatement("select field0003 from formmain_1872 where id=( select s.form_recordid from col_summary s where s.id = ?)");
				pst.setString(1, String.valueOf(summaryID));
				rs=pst.executeQuery();
				if(rs.next()){
					companyID=rs.getString(1);
				}
				System.err.println(String.valueOf(summaryID));
				System.err.println("公司ID："+companyID);
				
				DBUtil.close(pst);
				
				if(companyID.equals("-9095332753239945683")){//汽玻
					System.err.println("修改：");
				pst = conn
						.prepareStatement("update formmain_1872 f set f.field0023 = (select 'A' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

				pst.setString(1, String.valueOf(summaryID));

				pst.execute();
				System.err.println("修改提交");
				DBUtil.close(pst);

				pst = conn
						.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
				pst.execute();

				conn.commit();
				}else if(companyID.equals("4886522247480361050")){//万达
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'B' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("1984346453374488311")){//广州
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'C' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-7386440627376733956")){//上海
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'D' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-532128843520192539")){//天津
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'E' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-3056166803473416064")){//长春
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'F' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-5827028354185214362")){//万盛
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'G' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("6694150361441845707")){//沈阳
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'H' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("6243509099467588439")){//苏州
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'I' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-9159026259812135357")){//湖北
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'J' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-6079360189745249629")){//重庆
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'K' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}else if(companyID.equals("-7126498394857652158")){//郑州
					pst = conn
							.prepareStatement("update formmain_1872 f set f.field0023 = (select 'L' || to_char(sysdate, 'yymm') || decode(s.field02,to_char(sysdate, 'yyyy-mm'),lpad(s.serial+1, 3, 0),'001')from fy_zc_serial s where s.name = 'syserial' and s.field01 = 'A' ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");

					pst.setString(1, String.valueOf(summaryID));

					pst.execute();
					DBUtil.close(pst);
					pst = conn
							.prepareStatement(" update fy_zc_serial f set f.serial = decode(f.field02, to_char(sysdate, 'yyyy-mm'),f.serial+1,1),f.field02 = to_char(sysdate, 'yyyy-mm') where f.name = 'syserial' and f.field01 = 'A'");
					pst.execute();

					conn.commit();
				}
			} catch (Exception e) {
				DBUtil.rollback(conn);
				e.printStackTrace();
				log.error("实验申请单生成编号发生异常，summaryID为" + summaryID, e);
			} finally {
				DBUtil.setAutoCommit(conn, true);
				DBUtil.close(rs, pst, conn);
			}

			return success;
		}
	}
}
